package Product

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	"OnlineRetailers/models/User/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetProductById struct {
	beego.Controller
}

func (c *GetProductById) GetProductById() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Product/GetProductById"
	res := resMes.GetProductDetailed{}
	productDetailed := &Admin.ProductDetailed{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	id := c.GetString("id")

	//fmt.Println(begin)

	sql := "select * from `product_detailed` where `product_id`=?"
	err = o.Raw(sql, id).QueryRow(&productDetailed)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	if productDetailed.PrimariesImage != "" {
		res.CarouselImage = append(res.CarouselImage, productDetailed.PrimariesImage)
	}
	if productDetailed.CarouselImage1 != "" {
		res.CarouselImage = append(res.CarouselImage, productDetailed.CarouselImage1)
	}
	if productDetailed.CarouselImage2 != "" {
		res.CarouselImage = append(res.CarouselImage, productDetailed.CarouselImage2)
	}
	if productDetailed.DetailedImage1 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage1)
	}
	if productDetailed.DetailedImage2 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage2)
	}
	if productDetailed.DetailedImage3 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage3)
	}
	if productDetailed.DetailedImage4 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage4)
	}
	if productDetailed.DetailedImage5 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage5)
	}
	if productDetailed.DetailedImage6 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage6)
	}
	if productDetailed.DetailedImage7 != "" {
		res.DetailedImage = append(res.DetailedImage, productDetailed.DetailedImage7)
	}
	res.ProductDetailed = productDetailed
	res.Code = "200"
	c.Data["json"] = res
}
